package com.sdy.supermarket.task.notice;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.mvc.annotation.TaskSync;
import com.sdy.supermarket.biz.mapper.GsNoticeMapper;
import com.sdy.supermarket.biz.model.GsNotice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知定时任务类
 *
 * @author hyh
 */
@Component
@Slf4j
public class NoticeTask extends Thread {

    @Resource
    private GsNoticeMapper noticeMapper;

    /**
     * 根据通知的时间来修改通知的状态
     */
    @Scheduled(cron = "0/60 * * * * ?")
    @TaskSync
    public void queryNoticeTimeSetStatus() {
        List<GsNotice> all = new ArrayList<>();
        // 执行三条sql，根据当前时间判断，查询出符合对应条件的数据
        // 查询出来后过滤出已经符合当前状态的数据，只更改当前状态与数据库状态不相符的数据
        Date now = new Date();
        // 1.如果当前时间小于通知开始时间
        List<GsNotice> noStartList = noticeMapper.selectList(Wrappers.<GsNotice>lambdaQuery().gt(GsNotice::getNoticeStartTime, now)
                .select(GsNotice::getNoticeId, GsNotice::getNoticeStartTime, GsNotice::getNoticeEndTime, GsNotice::getNoticeStatus));
        noStartList = noStartList.stream().filter(start -> now.before(start.getNoticeStartTime()) && start.getNoticeStatus() != 0).collect(Collectors.toList());
        noStartList.forEach(start -> start.setNoticeStatus(0));
        // 2.如果当前时间大于通知开始时间小于通知结束时间
        List<GsNotice> startNoEndList = noticeMapper.selectList(Wrappers.<GsNotice>lambdaQuery().lt(GsNotice::getNoticeStartTime, now).gt(GsNotice::getNoticeEndTime, now)
                .select(GsNotice::getNoticeId, GsNotice::getNoticeStartTime, GsNotice::getNoticeEndTime, GsNotice::getNoticeStatus));
        startNoEndList = startNoEndList.stream().filter(noEnd -> now.after(noEnd.getNoticeStartTime()) && now.before(noEnd.getNoticeEndTime()) && noEnd.getNoticeStatus() != 1).collect(Collectors.toList());
        startNoEndList.forEach(noEnd -> noEnd.setNoticeStatus(1));
        // 3.如果当前时间大于通知结束时间
        List<GsNotice> endList = noticeMapper.selectList(Wrappers.<GsNotice>lambdaQuery().lt(GsNotice::getNoticeEndTime, now)
                .select(GsNotice::getNoticeId, GsNotice::getNoticeStartTime, GsNotice::getNoticeEndTime, GsNotice::getNoticeStatus));
        endList = endList.stream().filter(end -> now.after(end.getNoticeEndTime()) && end.getNoticeStatus() != 2).collect(Collectors.toList());
        endList.forEach(end -> end.setNoticeStatus(2));
        all.addAll(noStartList);
        all.addAll(startNoEndList);
        all.addAll(endList);
        if (all.size() == 0) {
            log.info("本次暂无需要更新状态的通知");
            return;
        }
        int update = noticeMapper.batchUpdate(all);
        if (update != 0) {
            log.info("更新通知状态成功");
        } else {
            log.error("更新通知状态");
        }
    }

}
